package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
import model.Bandwidth;
import model.Hub;
import model.IPAddress;
import model.Link;
import model.MacAddress;
import model.Node;
import model.Period;
import model.PhysicalNode;
import model.Property;
import model.XenNetworkInterface;
import model.XenNode;
import model.XenNetworkInterface.Type;
import model.exception.EbtablesException;
import model.exception.MacAddressException;
import model.exception.NotEnoughResourcesException;
import model.exception.PathEmulationException;
import model.exception.TcException;
import ssh.SSHCommandException;
import utils.IllegalValueException;
import utils.EnumType.UnitInfo;
import utils.EnumType.UnitTime;

public class Test {

public static void main(String[] args) throws MacAddressException, IllegalValueException, NotEnoughResourcesException, InterruptedException, EbtablesException, IOException, SSHCommandException, PathEmulationException {
		
		/* NODI FISICI CON INTERFACCIA PER CONNESSIONE SSH*/
		/* SERVER A*/
		Node serverA = new PhysicalNode("serverA", new UUID(1,0));
		serverA.setControlIP(new IPAddress(4, "192.168.74.16","255.255.255.0"));
		
		/* SERVER B*/
		PhysicalNode serverB = new PhysicalNode("serverB", new UUID(1,1));
		serverB.setControlIP(new IPAddress(4, "192.168.74.17","255.255.255.0"));
		
		/* HUB, NODI VIRTUALI CON INTERFACC e LINK*/
		Hub hub1 = new Hub("Hub1"); // Hub tra macchine 5 e 1
		Hub hub2 = new Hub("Hub2"); // Hub tra macchine 1 e 2
		Hub hub3 = new Hub("Hub3"); // Hub tra macchine 2, 3 e 4
//							                  ___
//							                 |   |
//							                 | 3 |
// ___			    ___		         ___   	 |___|
//|   |            |   |            |   |      | eth1
//| 5 |----hub1----| 1 |----hub2----| 2 |----hub3
//|___|eth1    eth1|___|eth2    eth2|___|eth1 _|_eth1
//        								     |   |
//        								     | 4 |
//		  								     |___|		
		
		/* MACCHINA VIRTUALE 1 (domU1) e le due interfacce eth1 e eth2*/
		XenNode dom1 = new XenNode("macchina1", new UUID(0,1));
		XenNetworkInterface eth1dom1 = new XenNetworkInterface("id01eth1",Type.bridge,new MacAddress("00:16:3E:01:03:01"),
																"vif-bridge", "vif-bridge",new Bandwidth(UnitInfo.megabit,2));
		Link linketh1dom1hub1 = new Link("eth1dom1hub1", eth1dom1, hub1,true);
		dom1.addInterface(eth1dom1);

		XenNetworkInterface eth2dom1 = new XenNetworkInterface("id01eth2",Type.bridge,new MacAddress("00:16:3E:01:03:02"),
																"vif-bridge", "vif-bridge",new Bandwidth(UnitInfo.megabit,5));
		Link linketh2dom1hub2 = new Link("eth2dom1hub2", eth2dom1, hub2,true);
		dom1.addInterface(eth2dom1);
		
		/* MACCHINA VIRTUALE 2 (domU2) e le due interfacce eth1 e eth2*/	
		XenNode dom2 = new XenNode("macchina2", new UUID(0,2));
		XenNetworkInterface eth1dom2 = new XenNetworkInterface("id02eth1",Type.bridge,new MacAddress("00:16:3E:02:03:01"),
																"vif-bridge", "vif-bridge",null);
		Link linketh1dom2hub3 = new Link("eth1dom2hub3", eth1dom2, hub3,true);
		dom2.addInterface(eth1dom2);
		
		XenNetworkInterface eth2dom2 = new XenNetworkInterface("id02eth2",Type.bridge,new MacAddress("00:16:3E:02:03:02"),
																"vif-bridge", "vif-bridge",new Bandwidth(UnitInfo.megabit,5));
		Link linketh2dom2hub2 = new Link("eth2dom2hub2", eth2dom2, hub2,true);
		dom2.addInterface(eth2dom2);
		
		/* MACCHINA VIRTUALE 3 (domU3) e l'interfaccia eth1*/
		XenNode dom3 = new XenNode("macchina3", new UUID(0,3));
		XenNetworkInterface eth1dom3 = new XenNetworkInterface("id03eth1",Type.bridge,new MacAddress("00:16:3E:03:02:01"),
																"vif-bridge", "vif-bridge",null);
		Link linketh1dom3hub3 = new Link("eth1dom3hub3", eth1dom3, hub3,true);
		dom3.addInterface(eth1dom3);
		
		/* MACCHINA VIRTUALE 4 (domU4) e l'interfaccia eth1*/
		XenNode dom4 = new XenNode("macchina4", new UUID(0,4));
		XenNetworkInterface eth1dom4 = new XenNetworkInterface("id04eth1",Type.bridge,new MacAddress("00:16:3E:04:02:01"),
																"vif-bridge", "vif-bridge",null);
		Link linketh1dom4hub3 = new Link("eth1dom4hub3", eth1dom4, hub3,true);
		dom4.addInterface(eth1dom4);

		/* MACCHINA VIRTUALE 5 (domU5)*/
		XenNode dom5 = new XenNode("macchina5", new UUID(0,5));
		XenNetworkInterface eth1dom5 = new XenNetworkInterface("id05eth1",Type.bridge,new MacAddress("00:16:3E:05:02:01"),
																"vif-bridge", "vif-bridge",new Bandwidth(UnitInfo.megabit,2));
		Link linketh1dom5hub1 = new Link("eth1dom5hub1", eth1dom5, hub1,true);
		dom5.addInterface(eth1dom5);
		
		/* BINDING HUB CON LINK*/
		hub1.addLink(linketh1dom1hub1);
		hub1.addLink(linketh1dom5hub1);
		
		hub2.addLink(linketh2dom1hub2);
		hub2.addLink(linketh2dom2hub2);
		
		hub3.addLink(linketh1dom2hub3);
		hub3.addLink(linketh1dom3hub3);
		hub3.addLink(linketh1dom4hub3);
		
		/* BINDING NODI FISICI CON NODI VIRTUALI*/
		/* I nodi 2 e 3 si trovano sul SERVER A. I nodi 1, 4 e 5 sul SERVER B. */
		serverA.addHostedNode(dom2);
		serverA.addHostedNode(dom3);
		serverB.addHostedNode(dom1);
		serverB.addHostedNode(dom4);
		serverB.addHostedNode(dom5);
		
		ArrayList<Hub> hubs = new ArrayList<Hub>();
		hubs.add(hub1);
		hubs.add(hub2);
		hubs.add(hub3);
//		
//		TopologyEmulation topology = new TopologyEmulation(hubs);
//		topology.enforce();
//		topology.destroy();
//		topology.reset();
//		
		
		hub1.setProperty(new Property(new Period(UnitTime.millisecond, 10),
										new Period(UnitTime.millisecond, 1),
										null,null,null));
		
		hub2.setProperty(new Property(new Period(UnitTime.millisecond, 5),
										null,null,null,null));
		
		hub3.setProperty(new Property(null,null,new Double(2),
										null,null));
////
		hub3.setBandwidth(new Bandwidth(UnitInfo.megabit, 2));
		hub2.setBandwidth(new Bandwidth(UnitInfo.megabit, 5));
//		
//		
//		
		PathEmulation path = new PathEmulation(hubs);
//		path.destroy();
//		path.reset();
		try {
//			path.destroy();
//			path.reset();
			path.enforce();
		} catch (TcException e) {
			e.printStackTrace();
		}
//		path.destroy();
//		path.reset();
		
		
//		oppure
//		Ebtables ebtables = new Ebtables(hub1);
		
		
		/* OOPERAZIONI ESEGUIBILI SULL'OGGETTO EBTABLES */
//		ebtables.reset();
//
//		ebtables.enforce();
//		ebtables.enforce();
//		
//		ebtables.destroy();
		
//		TopologyEmulation ebtables1 = new TopologyEmulation(hubs);
//		ebtables1.enforceTopology();
//		ebtables1.destroyTopology();
//		ebtables1.destroyTopology();
		
		
		
		
		/* Descrizione XML (da passare a LIBVIRT) per la creazione delle interfacce*/
//		System.out.print(eth1.getXMLDescription());
//		System.out.print(eth21.getXMLDescription());
//		System.out.print(eth22.getXMLDescription());
//		System.out.print(eth3.getXMLDescription());
//		System.out.print(eth4.getXMLDescription());
		
		
	
		
	}	
	
	
}
